package cn.j0n4than.api.service.impl;

import cn.j0n4than.api.entity.User;
import cn.j0n4than.api.mapper.UserMapper;
import cn.j0n4than.api.service.UserService;
import cn.j0n4than.api.utils.BcryptUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author jonathan
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    /**
     * 添加用户
     *
     * @param user User实体类
     * @return 影响行数
     */
    @Override
    public int insert(User user) {
        return this.baseMapper.insert(user);
    }

    /**
     * 删除用户
     *
     * @param id 用户ID
     * @return 影响行数
     */
    @Override
    public int delete(int id) {
        return this.baseMapper.deleteById(id);
    }

    /**
     * 更新用户信息
     *
     * @param id   用户ID
     * @param user 用户实体
     * @return 影响行数
     */
    @Override
    public int update(int id, User user) {
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("id", id);
        return this.baseMapper.update(user, userUpdateWrapper);
    }

    /**
     * 获取用户列表(分页)
     *
     * @param page  页码
     * @param limit 数据条数
     * @return result {total, pages}
     */
    @Override
    public HashMap<String, Object> getList(int page, int limit) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        HashMap<String, Object> result = new HashMap<>();
        userQueryWrapper
                .orderByDesc("created_at")
                .select("id",
                        "username",
                        "email",
                        "balance",
                        "last_login_ip",
                        "email_verified_at",
                        "last_login_at",
                        "created_at");
        this.baseMapper.selectList(userQueryWrapper);
        Page<User> p = new Page<>(page, limit);
        Page<User> userPage = this.baseMapper.selectPage(p, userQueryWrapper);

        result.put("list", userPage.getRecords());
        result.put("total", userPage.getTotal());
        result.put("current_page", page);
        result.put("limit", limit);
        return result;
    }

    /**
     * 通过ID找到用户
     *
     * @param id 用户ID
     * @return User 用户
     */
    @Override
    public User findUserById(int id) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("id", id);
        return this.baseMapper.selectOne(userQueryWrapper);
    }

    /**
     * 通过用户名找到用户
     *
     * @param username 用户名
     * @return User 用户
     */
    @Override
    public User findUserByUsername(String username) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", username);
        return this.baseMapper.selectOne(userQueryWrapper);
    }

    /**
     * 登陆验证
     *
     * @param username 用户名
     * @param password 密码
     * @return User 用户，验证失败返回null
     */
    @Override
    public User login(String username, String password) {
        User user = this.findUserByUsername(username);
        if (user == null) {
            return null;
        }

        if (!BcryptUtil.passwordVerify(password, user.password)) {
            return null;
        }
        return user;
    }
}
